/* $Header: glp.c,v 2.2 01/10/02 10:33:13 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  glp.c                                                 */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To provide functions for logging and reporting     */
/*                  glp exceptions.                                    */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <i_op.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
/* #include <p_config.h> */
#include <zone_config.h>
#include <config.h>
#include <zone_status.h>
#include <status.h>
#include <error.h>
#include <messages.h>
#include <zone_ui.h>

#include <stddef.h>
#include <std_ops.h>
#include <set.h>

#include <exception.h>
#include <err_handler.h>
#include <glp.h>

#include <beep.h>
#include <mio.h>
#include <cmd_set.h>
#include <method.h>
#include <hostutil.h>

#include <readiness.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/

/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/

/*****************************/
/*   LOCAL DEFINES           */
/*****************************/

/*****************************/
/*   MACROS                  */
/*****************************/

/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/


/***********************************************************************/
/* FUNCTION:  LogPendingExceptions                                     */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To log any active faults or warnings that occurred outside*/
/*           of a run.                                                 */
/*                                                                     */
/* RETURNED VALUE:  none                                               */
/*                                                                     */
/* GLOBALS USED:  Exception_Info table                                 */
/*                                                                     */
/* CALLS:  LogException                                                */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void LogPendingExceptions( void )
{
   EXCEPTION_WORDS *words;
   BIT32           mask;
   INT16           i;

   for ( i = 0; i < (INT16)LAST_EXCEPTION_NAME; i++ )
   {
      /* Only log the exceptions that occurred before a run and are not */
      /* repeatable.                                                    */

      if ( !Exception_Info[i].repeatable )
      {
         words = Exception_Info[ i ].words;
         mask  = Exception_Info[ i ].bit_mask;

         INTERRUPTS_OFF

         if ( ( words->status & mask ) &&
              ( Exception_Info[i].glp_msg_no != NO_MSG ) &&
              ( ( words->glp & mask ) == 0 ) )
         {
            /* Mark as logged */
            words->glp |= mask;

            INTERRUPTS_ON
            LogException( (EXCEPTION)i, 0, 0 );
         }
         else
         {
            INTERRUPTS_ON
         }
      }
   }
}

/***********************************************************************/
/* FUNCTION:  LogExternalDeviceNR                                      */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:  This function is only to be called prior to the       */
/*               start of a run.                                       */
/*                                                                     */
/***********************************************************************/

void LogExternalDeviceNR( void )
{
   if ( ExternalDeviceNotReady() )
   {
      /* Add new entry to the GLP log */

      /* Flag that there is an entry in the log */
      Glp_Status.deviations_found = TRUE;

      Glp_Log[ 0 ].dev_code = EXTERNAL_DEVICE_DEV;
      Glp_Log[ 0 ].header_msg_no = GLP_NOT_READY_MSG;
      Glp_Log[ 0 ].msg_no = MSG_EXTERNAL_NR;
      Glp_Log[ 0 ].value = 0;
      Glp_Log[ 0 ].value_2 = 0;
      Glp_Log[ 0 ].run_time = 0;

      /* Update the index to the next available entry location */

      Glp_Status.log_index++;
   }
}

/***********************************************************************/
/* FUNCTION:  LogSetptChange                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To log setpt changes during a run.                        */
/*                                                                     */
/* GLOBALS USED:  Glp_Status, Glp_Log                                  */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void LogSetptChange( SETPT setpt, INT32 value, INT32 value_2 )
{

   /* Force a readiness re-evaluation.  We have changed a setpoint that may   */
   /* effect readiness.                                                       */
   ClearNotReadyEval();

   /* For debug purposes, call a routine to beep the beeper, indicating that  */
   /* the setpoint change is being logged.                                    */
   if ( SetptBeepEnabled ) BEEP_SETPT_CHANGE;

   /* Clear memory reset warning  (VIREO) */
   ClearException (MEMORY_RESET);

   /* Set a flag that a parameter has been modified */
   if ( Setpt_Info[setpt].setpt_class == CALIB_SETPT )
   {
      Parameter_Modified[0].bits.gc_calib_param = 1;
      Parameter_Modified[1].bits.gc_calib_param = 1;
      Parameter_Modified2[(Cmd_Port==0)?1:0].bits.gc_calib_param = 1;
   }
   else
   {
      Method_Dirty = TRUE;
      Parameter_Modified[0].bits.gc_param = 1;
      Parameter_Modified[1].bits.gc_param = 1;
      Parameter_Modified2[(Cmd_Port==0)?1:0].bits.gc_param = 1;
   }

   /* If we are in a run or an scc run and are changing the active workfile   */
   /* then log the change.                                                    */
   if ( ( ACTIVE_WORKFILE ) && ( Run_State == RUN_ACTIVE ) )
   {
      /* Flag that there is an entry in the log */
      Glp_Status.deviations_found = TRUE;

      if ( Glp_Status.log_index < GLP_LOG_SIZE )
      {
         /* Add new entry to the GLP log */

         INTERRUPTS_OFF
         Glp_Log[ Glp_Status.log_index ].dev_code =
                                                 (DEV_CODE)Setpt_Info[setpt].glp_dev_code;
         Glp_Log[ Glp_Status.log_index ].header_msg_no =
                                                  Setpt_Info[setpt].glp_hdr_msg;
         Glp_Log[ Glp_Status.log_index ].msg_no = GetGlpSetptMsgNo(setpt, value_2);
         Glp_Log[ Glp_Status.log_index ].value = value;
         Glp_Log[ Glp_Status.log_index ].value_2 = value_2;
         Glp_Log[ Glp_Status.log_index ].run_time = GLP_RUNTIME;
         INTERRUPTS_ON

         /* Update the index to the next available entry location */

         Glp_Status.log_index++;

      }
      else
      {
         if ( Glp_Log[ GLP_LOG_SIZE].dev_code != (DEV_CODE)GLP_OVERFLOW )
         {
         INTERRUPTS_OFF
            LogException( GLP_OVERFLOW, GLP_LOG_SIZE, 0 );
         INTERRUPTS_ON
         }
      }
   }
}



/***********************************************************************/
/* FUNCTION:  LogException                                             */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To store an exception deviation in the GLP log.           */
/*                                                                     */
/* GLOBALS USED:  Glp_Status, Glp_Log                                  */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void LogException( EXCEPTION i, INT32 value, INT32 value_2 )
{
   /* Only log glp events when we are doing a run */
   if ( Run_State != RUN_ACTIVE ) return ;

   /* Flag that there is an entry in the log */
   Glp_Status.deviations_found = TRUE;

   if ( Glp_Status.log_index <= GLP_LOG_SIZE )
   {
      /* Check to see if we have overflowed the log */
      if ( Glp_Status.log_index == GLP_LOG_SIZE )
      {
         /* We have overflowed the log, change the  */
         /* exception number and log overflow !!!! */
         i =       GLP_OVERFLOW;
         value =   GLP_LOG_SIZE;
         value_2 = 0;
      }

      /* Add new entry to the GLP log */

      INTERRUPTS_OFF
      Glp_Log[ Glp_Status.log_index ].dev_code =
                                              (DEV_CODE)Exception_Info[i].glp_dev_code;
      Glp_Log[ Glp_Status.log_index ].header_msg_no =
                                              Exception_Info[i].glp_hdr_msg;
      Glp_Log[ Glp_Status.log_index ].msg_no = GetGlpMsgNo( i, value_2);
      Glp_Log[ Glp_Status.log_index ].value = value;
      Glp_Log[ Glp_Status.log_index ].value_2 = value_2;
      Glp_Log[ Glp_Status.log_index ].run_time = GLP_RUNTIME;

      /* Update the index to the next available entry location */

      Glp_Status.log_index++;

      INTERRUPTS_ON
   }

}

/***********************************************************************/
/* FUNCTION:  ClearGlpLogWords                                         */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To reset the flag bits used to mark an exception as being */
/*           logged.                                                   */
/*                                                                     */
/* GLOBALS USED: Not_Ready,Warning,Shutdown,Non_Fatal_Err_Glp_Log_Words*/
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void ClearGlpLogWords( void )
{
   /* Clear the bit-fields indicating a glp event has been logged */
   ClearGlpWords();

   Glp_Log[GLP_LOG_SIZE].dev_code = (DEV_CODE)LAST_EXCEPTION_NAME;
}

/***********************************************************************/
/* FUNCTION:  LogAlsParam                                              */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To set parameter modified bit for sampler param changes   */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void LogAlsParam( void )
{
/* NOTE: Run Log ignored for ALS as changes during run will not affect */
/*       current run.                                                  */

   if ( SetptBeepEnabled ) BEEP_SETPT_CHANGE;

   Method_Dirty = TRUE;
   Parameter_Modified[0].bits.gc_param    = 1;
/* Parameter_Modified[0].bits.smplr_param = 1;    */
   Parameter_Modified[1].bits.gc_param    = 1;
/* Parameter_Modified[1].bits.smplr_param = 1;    */
   Parameter_Modified2[(Cmd_Port==0)?1:0].bits.gc_param = 1;
}


/***********************************************************************/
/* FUNCTION:  LogSequenceParam                                         */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  Sets parameter modified bit for sequence parameter        */
/*           changes.                                                  */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void LogSequenceParam (void)
{

   /* Clear memory reset warning  (VIREO) */
   ClearException (MEMORY_RESET);

   /* Set a flag that a parameter has been modified */
   Parameter_Modified2[(Cmd_Port==0)?1:0].bits.seq_param = 1;
}


/***********************************************************************/
/* FUNCTION:  LogSequenceLoad                                          */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  Sets parameter modified bit for loading a sequence. It    */
/*           clears the seq param modified bit on all channels.        */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void LogSequenceLoad (void)
{
   /* Clear memory reset warning  (VIREO) */
   ClearException (MEMORY_RESET);

   /* Set a flag that a sequence has been loaded */
   Parameter_Modified2[(Cmd_Port==0)?1:0].bits.seq_loaded = 1;
   Parameter_Modified2[0].bits.seq_param = 0;
   Parameter_Modified2[1].bits.seq_param = 0;
}
